import java.util.*;

public class Solution {
    public static int trap(int[] height) {
        int count = 0;
        int n = height.length;
        int left = 0;
        // boolean flag = false;
        while (left < n) {
            int right = left + 1;
            // flag = false;
            int prev = count;
            while (right < n && height[right] <= height[left]) {
                count += (height[left] - height[right]);
                right++;
            }
            if (right == n) {
                left++;
                count = prev;
                continue;
            }
            left = right;

        }

        return count;
    }

    public static void main(String[] args) {
        trap(new int[]{0,1,0,2,1,0,1,3,2,1,2,1});
    }
}